package dianmobile.eistarandroid.usermanager;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

import dianmobile.eistarandroid.beans.UserInfo;
import dianmobile.eistarandroid.network.request.LoginRequest;
import dianmobile.eistarandroid.network.request.RequestCallback;
import dianmobile.eistarandroid.utils.ConstantsData;

/**
 * Created by admin on 15/7/2.
 */
public class LoginManager {
    private Context context;

    public LoginManager(Context context) {
        this.context = context;
        initLoginSetting();
    }

    private void initLoginSetting() {
        SharedPreferences sp = context.getSharedPreferences(ConstantsData.DB_PER, 0);
        hasSavedLoginData = sp.getBoolean(ConstantsData.K_BOOL_SAVED_LOGIN_DATA, false);
        if (hasSavedLoginData) {
            savedAccount = sp.getString(ConstantsData.K_SAVED_ACCOUNT, ConstantsData.EMPTY_STR);
            savedPassword = sp.getString(ConstantsData.K_SAVED_PASSWORD, ConstantsData.EMPTY_STR);
        }
        isAutoLogin = sp.getBoolean(ConstantsData.K_BOOL_AUTO_LOGIN, false);
    }

    public void notifyLoginSetting(String account, String password, boolean isRememberPassword, boolean isAutoLogin) {
        boolean isDataChanged = false;
        if (!account.equals(savedAccount)) {
            savedAccount = account;
            isDataChanged = true;
            userInfo().setUserId(savedAccount);
        }
        if (!password.equals(savedPassword)) {
            savedPassword = password;
            isDataChanged = true;
        }
        if (isRememberPassword != hasSavedLoginData) {
            hasSavedLoginData = true;
            isDataChanged = true;
        }
        if (isAutoLogin != this.isAutoLogin) {
            this.isAutoLogin = isAutoLogin;
            isDataChanged = true;
        }

        if (isDataChanged) {
            SharedPreferences sp = context.getSharedPreferences(ConstantsData.DB_PER, 0);
            sp.edit()
                    .putBoolean(ConstantsData.K_BOOL_AUTO_LOGIN, this.isAutoLogin)
                    .putBoolean(ConstantsData.K_BOOL_SAVED_LOGIN_DATA, this.hasSavedLoginData)
                    .putString(ConstantsData.K_SAVED_ACCOUNT, this.savedAccount)
                    .putString(ConstantsData.K_SAVED_PASSWORD, this.savedPassword)
                    .commit();
        }

        loginSuccess();
    }

    public void loginSuccess() {isLogin = true;}
//
//    public void active(final Context nowActivity, final Class<?> loginActivity) {
//        if (isLogin)
//            return;
//
//        initLoginSetting();
//
//        if (isAutoLogin) {
//            Map<String, String> params = LoginRequest.createParams(savedAccount, savedPassword);
//            LoginRequest.execute(context, params, new RequestCallback() {
//                @Override
//                public void onResult(Map<String, Object> result, String error) {
//                    if (error == null) {
//                        loginSuccess();     // auto login success
//                        Toast.makeText(context, "Auto Login Success", Toast.LENGTH_SHORT).show();
//                    }
//                    else {
//                        // auto login failed, then goto the login activity
//                        Intent gotoLoginActivity = new Intent();
//                        gotoLoginActivity.setClass(nowActivity, loginActivity);
//                        nowActivity.startActivity(gotoLoginActivity);
//                        ((Activity) nowActivity).finish();
//                    }
//                }
//            });
//        }
//        else {
//            // didn't set to auto login, and goto the login activity
//            Intent gotoLoginActivity = new Intent();
//            gotoLoginActivity.setClass(nowActivity, loginActivity);
//            nowActivity.startActivity(gotoLoginActivity);
//            ((Activity) nowActivity).finish();
//        }
//    }

    public void doAutoLogin(RequestCallback callback) {
        Map<String, String> params = LoginRequest.createParams(savedAccount, savedPassword);
        LoginRequest.execute(context, params, callback);
    }

    private boolean isAutoLogin;
    private boolean hasSavedLoginData;
    private String savedAccount;
    private String savedPassword;
    private static boolean isLogin = false;

    private UserInfo userInfo;

    public UserInfo userInfo() {
        if (userInfo == null)
            userInfo = new UserInfo();

        return userInfo;
    }

    public boolean isAutoLogin() {
        return isAutoLogin;
    }

    public void setIsAutoLogin(boolean isAutoLogin) {
        this.isAutoLogin = isAutoLogin;
    }

    public boolean isHasSavedLoginData() {
        return hasSavedLoginData;
    }

    public void setHasSavedLoginData(boolean hasSavedLoginData) {
        this.hasSavedLoginData = hasSavedLoginData;
    }

    public String getSavedAccount() {
        return savedAccount;
    }

    public void setSavedAccount(String savedAccount) {
        this.savedAccount = savedAccount;
    }

    public String getSavedPassword() {
        return savedPassword;
    }

    public void setSavedPassword(String savedPassword) {
        this.savedPassword = savedPassword;
    }

    public static boolean isLogin() {
        return isLogin;
    }

    public static void setIsLogin(boolean isLogin) {
        LoginManager.isLogin = isLogin;
    }
}
